package com.example.demo.webservice.service.employee;

import com.example.demo.data.employee.Employee;
import com.example.demo.data.employee.EmployeeBO;
import com.example.demo.webservice.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl implements EmployeeService{
    private EmployeeRepository employeeRepository;
    @Autowired
    public EmployeeServiceImpl(EmployeeRepository employeeRepository){
        this.employeeRepository = employeeRepository;
    }

    @Override
    public List<Employee> getAllEmployeeInfo() {
        return employeeRepository.getAllEmployeeInfo();
    }

    @Override
    public Employee getEmployeeInfoByEmpID(long empID) {
        return employeeRepository.getEmployeeInfoByEmpID(empID);
    }

    @Override
    public Employee addEmployeeInfo(Employee employee) {
        return employeeRepository.addEmployeeInfo(employee);
    }

    @Override
    public Employee editEmployeeInfo(Employee employee) {
        long empID = employee.getEmpID();
        Map<String, Object> empMap = employeeRepository.QueryByEmpID(empID);
        Employee employeeBO = new EmployeeBO();
        BOFilled(employeeBO, empMap);
        BOUpdate(employeeBO, employee);
        return employeeRepository.editEmployeeInfo(employeeBO);
    }

    @Override
    public ResponseEntity deleteEmployeeInfo(long empID) {
        return employeeRepository.deleteEmployeeInfo(empID);
    }
    public void BOFilled(Employee employeeBO, Map<String, Object> employeeMap){
        for(Map.Entry<String, Object> entry: employeeMap.entrySet()){
            switch (entry.getKey()){
                case "empId":
                    employeeBO.setEmpID((Long)entry.getValue());
                    break;
                case "empName":
                    employeeBO.setEmpName((String) entry.getValue());
                    break;
                case "deptId":
                    employeeBO.setDeptID((Long)entry.getValue());
                    break;
                case "deptName":
                    employeeBO.setDeptName((String) entry.getValue());
                    break;
                case "auth":
                    employeeBO.setAuth((Integer) entry.getValue());
                    break;
            }
        }
    }
    public void BOUpdate(Employee employeeBO, Employee employeeVO){
        if(employeeVO.getEmpName() != null){
            employeeBO.setEmpName(employeeVO.getEmpName());
        }
        if(employeeVO.getDeptID() != null){
            employeeBO.setDeptID(employeeVO.getDeptID());
        }
        if(employeeVO.getDeptName() != null){
            employeeBO.setDeptName(employeeVO.getDeptName());
        }
        if(employeeVO.getAuth() != null){
            employeeBO.setAuth(employeeVO.getAuth());
        }
    }
}
